




package com.yinhai.ta404.module.security.cas.entry;

import com.yinhai.ta404.core.restservice.resultbean.ResultBean;
import com.yinhai.ta404.core.utils.JsonFactory;
import com.yinhai.ta404.core.utils.NetUtils;
import com.yinhai.ta404.core.utils.ServiceLocator;
import com.yinhai.ta404.core.utils.ValidateUtil;
import com.yinhai.ta404.module.security.cas.utils.DESUtil;
import com.yinhai.ta404.module.security.cas.utils.ServerUtils;
import java.io.IOException;
import java.net.URLEncoder;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.jasig.cas.client.util.CommonUtils;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.security.cas.ServiceProperties;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.util.Assert;

public class TaCasAuthenticationEntryPoint implements AuthenticationEntryPoint, InitializingBean {
    private ServiceProperties serviceProperties;
    private String loginUrl;
    private boolean encodeServiceUrlWithSessionId = true;

    public TaCasAuthenticationEntryPoint() {
    }

    public void afterPropertiesSet() throws Exception {
        Assert.hasLength(this.loginUrl, "loginUrl must be specified");
        Assert.notNull(this.serviceProperties, "serviceProperties must be specified");
    }

    public final void commence(HttpServletRequest servletRequest, HttpServletResponse response, AuthenticationException authenticationException) throws IOException, ServletException {
        String urlEncodedService = this.createServiceUrl(servletRequest, response);
        String sessionId = ";jsessionid";
        if (urlEncodedService.contains(sessionId)) {
            urlEncodedService = urlEncodedService.substring(0, urlEncodedService.indexOf(sessionId));
        }
        sessionId = ";JSESSIONID";
        if (urlEncodedService.contains(sessionId)) {
            urlEncodedService = urlEncodedService.substring(0, urlEncodedService.indexOf(sessionId));
        }

        this.preCommence(servletRequest, response);
        ResultBean resultBean = (ResultBean)ServiceLocator.getAppContext().getBean(ResultBean.class);
        String redirectUrl = null;

        try {
            redirectUrl = this.createRedirectUrl(URLEncoder.encode(DESUtil.encrypt(urlEncodedService, "toi3wfexhms="), "UTF-8"));
            String frontUrl = servletRequest.getParameter("frontUrl");
            if (!ValidateUtil.isNotEmpty(frontUrl)) {
                resultBean.addError("500", "frontUrl参数缺失!");
                resultBean.setSuccess(false);
                response.setContentType("application/json;charset=UTF-8");
                response.getWriter().write(JsonFactory.bean2json(resultBean));
                return;
            }

            frontUrl = URLEncoder.encode(DESUtil.encrypt(frontUrl, "toi3wfexhms=").replaceAll("\\r|\\n", ""), "UTF-8");
            redirectUrl = redirectUrl + "%3Fret=" + frontUrl;
            String ip = NetUtils.getLocalHost();
            int port = ServerUtils.getPort();
            String stService = "http://" + ip + ":" + port + servletRequest.getContextPath() + "/";
            stService = DESUtil.encrypt(stService, "toi3wfexhms=");
            if (redirectUrl.contains("?")) {
                redirectUrl = redirectUrl + "&stService=" + URLEncoder.encode(stService, "UTF-8");
            } else {
                redirectUrl = redirectUrl + "?stService=" + URLEncoder.encode(stService, "UTF-8");
            }
        } catch (Exception var12) {
            var12.printStackTrace();
        }

        resultBean.addError("403", "未登录");
        resultBean.setRedirectUrl(redirectUrl);
        resultBean.setSuccess(false);
        response.setContentType("application/json;charset=UTF-8");
        response.getWriter().write(JsonFactory.bean2json(resultBean));
    }

    protected String createServiceUrl(HttpServletRequest request, HttpServletResponse response) {
        return CommonUtils.constructServiceUrl((HttpServletRequest)null, response, this.serviceProperties.getService(), (String)null, this.serviceProperties.getArtifactParameter(), this.encodeServiceUrlWithSessionId);
    }

    protected String createRedirectUrl(String serviceUrl) {
        return CommonUtils.constructRedirectUrl(this.loginUrl, this.serviceProperties.getServiceParameter(), serviceUrl, this.serviceProperties.isSendRenew(), false);
    }

    protected void preCommence(HttpServletRequest request, HttpServletResponse response) {
    }

    public final String getLoginUrl() {
        return this.loginUrl;
    }

    public final void setLoginUrl(String loginUrl) {
        this.loginUrl = loginUrl;
    }

    public final ServiceProperties getServiceProperties() {
        return this.serviceProperties;
    }

    public final void setServiceProperties(ServiceProperties serviceProperties) {
        this.serviceProperties = serviceProperties;
    }

    protected boolean getEncodeServiceUrlWithSessionId() {
        return this.encodeServiceUrlWithSessionId;
    }

    public final void setEncodeServiceUrlWithSessionId(boolean encodeServiceUrlWithSessionId) {
        this.encodeServiceUrlWithSessionId = encodeServiceUrlWithSessionId;
    }
}
